package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.entity.ShoppingCart;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrderMapper {

    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into orders" +
            "        (number, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status, amount, remark," +
            "         phone, address, consignee, estimated_delivery_time, delivery_status, pack_amount, tableware_number," +
            "         tableware_status)" +
            "        values (#{number}, #{status}, #{userId}, #{addressBookId}, #{orderTime}, #{checkoutTime}, #{payMethod}," +
            "                #{payStatus}, #{amount}, #{remark}, #{phone}, #{address}, #{consignee}," +
            "                #{estimatedDeliveryTime}, #{deliveryStatus}, #{packAmount}, #{tablewareNumber}, #{tablewareStatus})")
    void insert(Orders orders);

    /**
     * 分页条件查询并按下单时间排序
     * @param od
     */
    Page<Orders> pageQueryH(OrdersPageQueryDTO od);

    /**
     * 根据id查询订单
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);

    /**
     * 修改订单
     * @param orders
     */
    @Update("update orders set status = #{status},cancel_reason = #{cancelReason},cancel_time = #{cancelTime},rejection_reason = #{rejectionReason},delivery_time = #{deliveryTime} where id = #{id}")
    void updateOrders(Orders orders);

    /**
     * 根据状态统计订单数量
     * @param status
     */
    @Select("select count(id) from orders where status = #{status}")
    Integer countStatus(Integer status);

    @Select("select sum(amount) from orders where status = 5 and order_time between #{start} and #{stop}")
    Double sumAmountByDate(LocalDateTime start, LocalDateTime stop);

    Integer countStatistics(LocalDateTime start, LocalDateTime stop, Integer status);

    /**
     * 查询商品销量前10名
     */
    List<GoodsSalesDTO> salesTop10(LocalDateTime start, LocalDateTime stop);
}
